# -*- coding: utf-8 -*-
from openerp.osv import osv, fields
import logging

class vng_promotion_email(osv.osv):
    _name = "vng.promotion.email"
    __logger = logging.getLogger(_name)
    _auto = False

    #===========================================================================
    # How to use email template
    # try:
    #        # add function to Send mail here
    #        name = "vng_shop123_promotion_submit"
    #        pEmail = self.pool.get('vng.promotion.email')
    #        rs = pEmail.vng_email_template_send_email(cr, uid, ids, name, context)
    #        
    #    except Exception as exc:
    #        self.__logger.error('Error: %s' % exc, exc_info=True)
    #===========================================================================

    def vng_email_template_send_email(self, cr, uid, object_ids, name_template=False, context=None):
        self.__logger.info('START: vng_email_template_send_email')
        result = {}
        pEmailTemplate = self.pool.get('email.template')
        emailtemplate_ids = pEmailTemplate.search(cr, uid, [('name', '=', name_template)])
        if len(emailtemplate_ids):
            cr.commit()
            emailtemplate_id = emailtemplate_ids[0]
            for object_id in object_ids:
                try:

                    result[object_id] = pEmailTemplate.send_mail(cr, uid, emailtemplate_id, object_id, False,
                                                                 context=context)
                except Exception as exc:

                    self.__logger.info('Can NOT send email %s' % (exc))
                    result[object_id] = False
        else:
            self.__logger.info('Can NOT find the email %s' % (name_template))
        self.__logger.info('END: vng_email_template_send_email')
        return result


vng_promotion_email()